﻿Cn_Regbus = {
	//注册更多信息初始化
    init : function(){
    	if(!Jes.userSess(1))return;
    	$.formValidator.initConfig({formid:"form1",onerror:function(){	}});
		$("#comment").formValidator({onshow:"用一句话介绍商家",onfocus:"至少6个字符,最多250个字符",oncorrect:"输入正确"}).inputValidator({min:6,max:250,onerror:"你输入的长度不正确,请确认"});
		$("#LEGAL").formValidator({onshow:"工商注册法人代表姓名",onfocus:"至少2个字符",oncorrect:"输入正确"}).inputValidator({min:2,max:30,onerror:"你输入的长度不正确,请确认"});
		$("#LEGALID").formValidator({onshow:"工商注册法人身份证号",onfocus:"至少2个字符",oncorrect:"输入正确"}).inputValidator({min:2,max:30,onerror:"你输入的长度不正确,请确认"});
		$("#COMMID").formValidator({onshow:"工商注册号",onfocus:"至少10个字符",oncorrect:"输入正确"}).inputValidator({min:10,max:16,onerror:"你输入的长度不正确,请确认"});
		$("#ADDRESS").formValidator({onshow:"商家的详细地址",onfocus:"至少5个字符",oncorrect:"输入正确"}).inputValidator({min:5,max:60,onerror:"地址不能为空"});
		$("#TEL").formValidator({onshow:"用于用户联系咨询方式",onfocus:"电话位数不对",oncorrect:"输入正确"}).inputValidator({min:5,max:60,onerror:"电话位数不对"});
		
    	 Cn_Regbus.query();
    	 Jes.initMap('map_canvas');
    	 this.initCKEditor('');
    }
    ,verify_business_name: function() {
        var name = Jes('name').value;
        Jes.doQueryList('vername_query_sql@bus', {paraObj: '{name:"' + name + '"}'}, function(data) {
            if (data[0].num > 0) {
                alert('此账号已注册');
            }
        });
    }
	//回车 激动按钮
	,onCenter : function(){
		alert(0);
		//	if(event.keyCode ==13){
		//		alert(1);
		//	}
	//	Cn_Regbus.reg(0);
	}
	//show ckedit
	,test : function(){
		Jes.html('intro','<html>good things</html>');
        var opts = {
            //filebrowserImageBrowseUrl: '/browser/browse.php?type=Images',
          //  filebrowserImageUploadUrl: '/uploader/upload.php?type=Images'
          	filebrowserImageBrowseUrl: '../../browse.shtml?type=buss',
            filebrowserImageUploadUrl: '../../upload.shtml?type=buss'
        };
        CKEDITOR.replace('intro', opts);
	}
	,testbtn : function(){
		var intro = CKEDITOR.instances.intro.getData();
		alert(intro);
	}
    ,initCKEditor: function(intro) {
        Jes.html('intro',intro);
        //alert(intro);
        var opts = {
            filebrowserImageBrowseUrl: '/browser/browse.php?type=Images',
            filebrowserImageUploadUrl: '/uploader/upload.php?type=Images'
        };
        CKEDITOR.replace('intro', opts);
    },
    locate: function() {
    	var address = Jes('area')[Jes('area').selectedIndex].text.trim()+Jes('city')[Jes('city').selectedIndex].text.trim()+Jes('address').value;
    	Jes.locate(address,'map_canvas');
    }
    //初始化状态
    ,query :  function() {
        var busid = Jes.request('busid');
        if(busid!=null&&busid!=''){//修改
			Cn_Regbus.viewbus(busid);
        }else{//新用户注册  用户注册成商家
        	var userid = Jes.request('userid');
       		var qrid = Jes.request('id');
        	Jes.doSaveTObject('cn.verify', {'isSess':0,param:{'id':qrid}}, function(result) {
        		Jes.doQueryList('getbusByuserid_query_sql@bus',{isLoad:false},function(data){
        			if(data.length==0){
        			Jes('userid').value = userInfo.userid;
                	Jes('email').value = userInfo.email;
	                Jes.doSync(function() {//异步函数
	                    Jes.Select.area();
	                    Jes.Select.city();
	                	});
	                	Jes.Select.ind();
        			}else{
        				Cn_Regbus.viewbus(data[0].id);
        			}
        		})
            });
        }
    }
    ,viewbus : function(busid){
    	Jes.doQueryList('busconfirm_query_sql@bus',{isSess:0,paraObj:'{busid:"' + busid + '"}',isLoad:false},function(data){
           	Jes.doSync(function(){//异步函数
				Jes.Select.area();
				Jes('area').value=data[0].area;
				if(Jes('area').value!='') Jes.Select.city();
				Jes('city').value=data[0].city;
				Jes.Select.ind();
				Jes('ind').value=data[0].industry;
				Jes('busname').value = data[0].name;
				Jes.setFormValue('form1',data[0]);
				var el = Jes('upImg');
				try{
					var src = CONFIG_DATA.path + data[0].pic;
					Jes('pic').value=data[0].pic;
					Jes.html('upImg','<table><tr><td><img width="150" height="150" id="act_pic" src="'+src+'"></td><td><a href="javascript:Jes.Upload.del();">删除</a></td></tr></table>');
			    	Cn_Regbus.initCKEditor(data[0].intro);
			    }catch(e){
					Jes.alert("获得附件信息失败，请联系管理员。");
				}
			});
        });
    }
    //商家注册按钮
    ,reg : function(isBus) {
        var email = Jes('email').value;
        if(Jes('password').value.length<6){Jes.alert('密码至少六位');return;}
        Jes.doQueryList('verify_query_sql@bus', {isSess:0,paraObj:'{email:"' + email + '"}',isLoad:false}, function(data) {
            if (data[0].num > 0) {
                alert("该邮箱已被注册，请换其他邮箱地址。");
                window.location.reload;
            } else {
                Jes.query('reverify_query_sql@bus', {paraObj:'{isSess:0,email:"' + email + '"}',isLoad:false}, function(data) {
                    if (data[0].num > 0) {
                        alert("该邮箱已被注册，请到邮箱激活账号。");
                        var eService = Cn_Regbus.getmailservice(email);
                    } else {
                        var password = Jes('password').value;
                        var repassword = Jes('repassword').value;
                        if (password == repassword) {
                            Jes.doSaveTObject('CnUser', {isSess:0,isLoad:false}, function(data) {
                                var eService = Cn_Regbus.getmailservice(email);
                                Jes.html('content', '<h1>查收确认信</h1><div class="grid-16-8 clearfix"><div class="article">' +
                                    '<p class="pl">确认信已经发到你的邮箱 <span id="email">' + email + '</span> ，你需要点击邮件中的确认链接来完成注册。</p>' +
                                    '<h2>没有收到确认信怎么办？······</h2><ol><li>检查一下上面Email地址是否正确，错了就<a href="/cuniao/cuniao/accounts/register">重新注册</a>一次吧:)</li>' +
                                    '<li>看看是否在邮箱的垃圾箱里</li><li>稍等几分钟，若仍旧没收到确认信，让簇鸟<a href="" id="a_resend">重发一封</a></ol>');
                            });
                        } else {
                            alert("密码输入不一致");
                        }
                    }
                });
            }
        });
    }
    //邮箱服务器地址
    ,getmailservice : function(email) {
        var i = email.indexOf('@');
        var eService = 'http://mail.' + email.substring(i + 1);
        return eService;
    }
    //保存商家信息
    ,savebus : function() {
    	if(!$.formValidator.pageIsValid('1'))return;
    //	if(Jes('COMMID').value.length<13){alert('工商执照号输入错误');Jes('COMMID').focus();return;}
        var param = DWRExtUtil.getFormValue("form1",1);
        var intro = CKEDITOR.instances.intro.getData();
        Jes('intro').value = intro;
        Jes('name').value = Jes('busname').value;
        if(Jes('pic').value==null||Jes('pic').value==''){alert('logo图片必须上传');return;}
        Jes('industry').value = Jes('ind').value;
        var dd;
        Jes.doSync(function(){Jes.doSaveTObject('CnBusiness', '', function(data){dd = data;});});
        if (dd == '') {
        	alert('操作失败');
        } else {
            window.location.href = '../bus/busconfirm.html?busid='+dd;
        }
    }
    //到打印页面
    ,gotoprint : function(){
    	//修改商家提交申请的状态
    	var busid = Jes.request('busid');
    	Jes.executeSql({ids:busid,sql:'chbusstatus_update_hql'},function(){
			window.open('./bus/busprint.html?busid='+busid);
		});
    }
	
}